package cn.edu.hnu.gpg.service;

import cn.edu.hnu.gpg.dao.SupervisionMapper;
import cn.edu.hnu.gpg.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SupervisionServiceImpl implements SupervisionService{

    private SupervisionMapper supervisionMapper;

    @Autowired
    private  StudentService studentService;
    @Autowired
    private  TeacherService teacherService;
    @Autowired
    public SupervisionServiceImpl(SupervisionMapper supervisionMapper){
        this.supervisionMapper = supervisionMapper;
    }

    @Override
    public boolean createSupervision(User user, String teacher_no) {
        int student_id = studentService.findStudentByUserId(user.getUserId()).getStudentId();

        Supervision supervisions = supervisionMapper.selectByPrimaryKey(student_id);
        if (supervisions != null)
            throw new IllegalArgumentException("该学生已有导师！！！");
        Teacher teacher = teacherService.findTeacherByTeacherNo(teacher_no);
        if (teacher == null)
            throw new IllegalArgumentException("这是一个无效的工号！！！");
        Supervision supervision = new Supervision();
        supervision.setStudentId(student_id);
        supervision.setTeacherId(teacher.getTeacherId());
        return supervisionMapper.insertSelective(supervision) > 0 ;
    }

    @Override
    public boolean modifSupervision(int student_id, int teacher_id) {
        if (student_id <1 || teacher_id < 1)
            return false;

        Supervision supervision = new Supervision();
        supervision.setStudentId(student_id);
        supervision.setTeacherId(teacher_id);

        return supervisionMapper.updateByPrimaryKey(supervision) > 0;
    }

    @Override
    public boolean deleteSupervision(int student_id) {
        if (student_id <1 )
            return false;

        return supervisionMapper.deleteByPrimaryKey(student_id) > 0;
    }

    @Override
    public int findTeacherByStudent(Student student) {
        if (student == null)
            return 0;

        Supervision supervision = supervisionMapper.selectByPrimaryKey(student.getStudentId());
        return supervision.getTeacherId();
    }

    @Override
    public List<Supervision> findSupervisionByTeacherId(Teacher teacher) {
        if (teacher == null)
            return null;

        SupervisionExample supervisionExample = new SupervisionExample();
        supervisionExample.or().andTeacherIdEqualTo(teacher.getTeacherId());
        List<Supervision> supervisions = supervisionMapper.selectByExample(supervisionExample);
        return supervisions.size()>0 ? supervisions : null;
    }

    @Override
    public List<Integer> findStudentIdByTeacher(Teacher teacher) {
        if (teacher == null)
            return null;

        SupervisionExample supervisionExample = new SupervisionExample();
        supervisionExample.or().andTeacherIdEqualTo(teacher.getTeacherId());
        List<Supervision> supervisions = supervisionMapper.selectByExample(supervisionExample);
        int len = supervisions.size();
        if (len < 1)return null;
        List<Integer> studentIdList = new ArrayList<>();
        for(Supervision supervision: supervisions){
            studentIdList.add(supervision.getStudentId());
        }
        return  studentIdList;
    }
}
